//
//  CarsCubeViewController.m
//  CarsCube
//
//  Created by Wander See on 11-3-17.
//  Copyright 2011年 Tongji Apple Club. All rights reserved.
//

#import "CarsCubeViewController.h"
#import <QuartzCore/QuartzCore.h>

#import "GameEngine.h"
#import "CC3DemoMashUpLayer.h"
#import "CC3DemoMashUpWorld.h"

#import "CBArena.h"

@implementation CarsCubeViewController

#pragma mark - init

- (void)initViews {
    
    CGRect windowRect = CGRectMake(0, 0, 768, 1024);
    
    // Add subviews.
    [self.view addSubview:vwLevels_];
    [self.view addSubview:vwGameCenter_];
    [self.view addSubview:vwOptions_];
    [self.view addSubview:vwCredits_];
    [self.view addSubview:vwGame_];
    [self.view addSubview:vwPause_];
    [self.view addSubview:vwMain_];
    
    vwMain_.frame = windowRect;
}

- (void)initGameCenter {
    
}

#pragma mark - Private Methods

- (void)aniWater {
	CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.5f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.removedOnCompletion = NO;
	animation.type = @"rippleEffect";
	[self.view.layer addAnimation:animation forKey:@"animationID"]; 
}

#pragma mark - Normal

- (void)dealloc {
    [vwMain_ removeFromSuperview];
    [vwLevels_ removeFromSuperview];
    [vwGameCenter_ removeFromSuperview];
    [vwOptions_ removeFromSuperview];
    [vwCredits_ removeFromSuperview];
    [vwGame_ removeFromSuperview];
    [vwPause_ removeFromSuperview];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

//#define DEBUG_GAME

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
#ifdef DEBUG_GAME
    [self.view addSubview:[GameEngine getEngineView]];
    [GameEngine startCocos];
#else
    [vwGame_ addSubview:[GameEngine getEngineView]];
    [vwGame_ bringSubviewToFront:btPause_];
    [vwGame_ bringSubviewToFront:slX_];
    [vwGame_ bringSubviewToFront:slY_];
    [vwGame_ bringSubviewToFront:slZ_];
    [self initViews];
#endif
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        return YES;
    }
    return NO;
}

#pragma mark - Events

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    //NSLog(@"VC touchesEnded view %@.", aTouch.view);
    
    if (aTouch.view == vwLevels_) {
        [self.view sendSubviewToBack:vwLevels_];
    } else if (aTouch.view == vwGameCenter_) {
        [self.view sendSubviewToBack:vwGameCenter_];
    } else if (aTouch.view == vwOptions_) {
        [self.view sendSubviewToBack:vwOptions_];
    } else if (aTouch.view == vwCredits_) {
        [self.view sendSubviewToBack:vwCredits_];
    }
}

#pragma mark - IBActions

// Main Menu

- (IBAction)btStartClicked:(id)sender {
    [self.view bringSubviewToFront:vwLevels_];
}

- (IBAction)btOnlineClicked:(id)sender {
    // 这里要配对了。
}

- (IBAction)btGameCenterClicked:(id)sender {
    [self.view bringSubviewToFront:vwGameCenter_];
}

- (IBAction)btOptionsClicked:(id)sender {
    [self.view bringSubviewToFront:vwOptions_];
}

- (IBAction)btCreditsClicked:(id)sender {
    [self.view bringSubviewToFront:vwCredits_];
}

// Levels

- (IBAction)btRaceClicked:(id)sender {
    [GameEngine startCocos];
    [self.view bringSubviewToFront:vwGame_];
}

// Game Center

- (IBAction)btGameCenterAchievements:(id)sender {
}

- (IBAction)btGameCenterLeaderboardsClicked:(id)sender {
}

- (IBAction)btGameCenterSubmitClicked:(id)sender {
}

// Credits

- (IBAction)btCreditsMoreAppsClicked:(id)sender {
}

- (IBAction)btCreditsRateMeClicked:(id)sender {
}

- (IBAction)btCreditsTellFriendsClicked:(id)sender {
}

// Game

- (IBAction)btPauseClicked:(id)sender {
    [[CCDirector sharedDirector] pause];
    [self.view bringSubviewToFront:vwPause_];
    
    //[(CBArena *)[GameEngine currentWorld] rotateSomex:0 y:50 z:0];
}

- (IBAction)slideLayer {
    [(CBArena *)[GameEngine currentWorld] rotateSomex:100*slX_.value y:100*slY_.value z:100*slZ_.value];
}

// Pause

- (IBAction)btPauseRestartClicked:(id)sender {
    [GameEngine startCocos];
    [self.view bringSubviewToFront:vwGame_];
}

- (IBAction)btPauseResumeClicked:(id)sender {
    [[CCDirector sharedDirector] resume];
    [self.view sendSubviewToBack:vwPause_];
}

- (IBAction)btPauseBackMainClicked:(id)sender {
    [[CCDirector sharedDirector] pause];
    [self.view bringSubviewToFront:vwMain_];
}

@end
